﻿using AutoMapper;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.App.Application.Dto.Input;
using Traceability.App.Application.Dto.Output;
using Traceability.App.Domain.Model;
using Traceability.App.ErrorCode;

namespace Traceability.App.Application
{
    public class Service : IService
    {
        private readonly IBaseRepository<ProductModel> _productRepository;
        private readonly IBaseRepository<TraceabilityModel> _traceRepository;
        private readonly IMapper _mapper;
        private readonly IBaseRepository<CultivationModel> _cultivationRepository;
        private readonly IBaseRepository<HealthModel> _healthRepository;
        private readonly IBaseRepository<Cultivation_TableModel> _cultivation_TableRepository;
        private readonly IBaseRepository<SlaughterModel> _slaughterRepository;
        private readonly IBaseRepository<Slaughter_TableModel> _slaughter_TableRepository;
        private readonly IBaseRepository<SlaughterhouseModel> _SlaughterhouseRepository;
        private readonly IBaseRepository<SupplierModel> _supplierRepository;
        private readonly IBaseRepository<ImageModel> _imgRepository;
        public Service(IBaseRepository<ProductModel> productRepository, IBaseRepository<TraceabilityModel> traceRepository, IMapper mapper, IBaseRepository<CultivationModel> cultivationRepository, IBaseRepository<HealthModel> healthRepository, IBaseRepository<Cultivation_TableModel> cultivation_TableRepository, IBaseRepository<SlaughterModel> slaughterRepository, IBaseRepository<Slaughter_TableModel> slaughter_TableRepository, IBaseRepository<SlaughterhouseModel> slaughterhouseRepository, IBaseRepository<SupplierModel> supplierRepository, IBaseRepository<ImageModel> imgRepository)
        {
            _productRepository = productRepository;
            _traceRepository = traceRepository;
            _mapper = mapper;
            _cultivationRepository = cultivationRepository;
            _healthRepository = healthRepository;
            _cultivation_TableRepository = cultivation_TableRepository;
            _slaughterRepository = slaughterRepository;
            _slaughter_TableRepository = slaughter_TableRepository;
            _SlaughterhouseRepository = slaughterhouseRepository;
            _supplierRepository = supplierRepository;
            _imgRepository = imgRepository;
        }
        /// <summary>
        /// 获取商品名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<List<ProductModel>> GetProductShow()
        {
            ApiResult<List<ProductModel>> res = new ApiResult<List<ProductModel>>();
            var model = _productRepository.GetAll();
            res.Code=ApiEnums.OK;
            res.Message="查询成功";
            res.Data=model.ToList();
            return res;
        }
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddProduct(AddProductInput input)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model=_mapper.Map<TraceabilityModel>(input);
            model.CreateUserName = "张三";
            model.UpdateUserName = "李四";
            int i=_traceRepository.Create(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "添加成功";
                res.Data = i;
            }
            else 
            {
                res.Code = ApiEnums.Fail;
                res.Message = "添加失败";
            }
            return res;
        }
        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateProduct(UpdateProductInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = _mapper.Map<TraceabilityModel>(input);
            model.CreateUserName = "张三";
            model.UpdateUserName = "李四";
            int i = _traceRepository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "修改成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Fail;
                res.Message = "修改失败";
            }
            return res;
        }
        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<int> DeleteProduct(int id)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = _traceRepository.GetAll().FirstOrDefault(x => x.IsDeleted == false);
            model.IsDeleted = true;
            int i = _traceRepository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else 
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除失败";
                
            }
            return res;
        }

        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiPaging<ProductOutput> GetTraceabilityShow(PageInput input) 
        {
            ApiPaging<ProductOutput> res = new ApiPaging<ProductOutput>();
            var model = _traceRepository.GetAll().Where(x=>x.IsDeleted==false);
            var product = _productRepository.GetAll().Where(x=>x.IsDeleted==false);
            var cultivation = _cultivationRepository.GetAll().Where(x=>x.IsDeleted==false);
            var cultivation_table = _cultivation_TableRepository.GetAll().Where(x=>x.IsDeleted==false);
            var slaughter = _slaughterRepository.GetAll().Where(x=>x.IsDeleted==false);
            var slaughter_table = _slaughter_TableRepository.GetAll().Where(x=>x.IsDeleted==false);
            var slaughterhouse = _SlaughterhouseRepository.GetAll().Where(x=>x.IsDeleted==false);

            var linq = from a in model
                       join b in product on a.ProductId equals b.Id into ab
                       from b in ab.DefaultIfEmpty()
                       join c in cultivation on a.Batch equals c.Batch into ac
                       from c in ac.DefaultIfEmpty()
                       join d in cultivation_table on a.Batch equals d.Batch into ad
                       from d in ad.DefaultIfEmpty()
                       join e in slaughter on a.Batch equals e.Batch into ae
                       from e in ae.DefaultIfEmpty()
                       join f in slaughter_table on a.Batch equals f.Batch into af
                       from f in af.DefaultIfEmpty()
                       join g in slaughterhouse on a.Batch equals g.Batch into ag
                       from g in ag.DefaultIfEmpty()
                       select new ProductOutput
                       {
                           Id = a.Id,
                           ProductId = a.ProductId,
                           ProductName = b.ProductName,
                           Batch = a.Batch,
                           Shelf_life = a.Shelf_life,
                           License = a.License,
                           Enterprise = a.Enterprise,
                           CreateUserName = a.CreateUserName,
                           UpdateUserName = a.UpdateUserName,
                           CreateTime = a.CreateTime,
                           UpdateTime = a.UpdateTime,
                           Cover = a.Cover,
                           Completeness = Convert.ToInt32(
                               // 基本信息模块 (10%)
                               ((!string.IsNullOrEmpty(b.ProductName) ? 5 : 0) +
                               (!string.IsNullOrEmpty(a.Batch) ? 5 : 0)) +
                               // 养殖信息模块 (30%)
                               (c != null ? (
                                   (!string.IsNullOrEmpty(c.Breed) ? 6 : 0) +
                                   (!string.IsNullOrEmpty(c.Source) ? 6 : 0) +
                                   (c.Days.HasValue ? 6 : 0) +
                                   (!string.IsNullOrEmpty(c.Brand) ? 6 : 0) +
                                   (!string.IsNullOrEmpty(c.Caretaker) ? 6 : 0)
                               ) : 0) +
                               // 屠宰信息模块 (30%)
                               (e != null ? (
                                   (e.CounterBalance.HasValue ? 7.5m : 0) +
                                   (e.ArrivalTime.HasValue ? 7.5m : 0) +
                                   (e.Slaughter_Time.HasValue ? 7.5m : 0) +
                                   (!string.IsNullOrEmpty(e.Batch) ? 7.5m : 0)
                               ) : 0) +
                               // 深加工信息模块 (30%)
                               (g != null ? (
                                   (!string.IsNullOrEmpty(g.CompanyName) ? 6 : 0) +
                                   (!string.IsNullOrEmpty(g.License_Number) ? 6 : 0) +
                                   (!string.IsNullOrEmpty(g.Team) ? 6 : 0) +
                                   (!string.IsNullOrEmpty(g.Principal) ? 6 : 0) +
                                   (!string.IsNullOrEmpty(g.Batch) ? 6 : 0)
                               ) : 0)).ToString()
                       };

            if (input.ProductId>0)
            {
                linq = linq.Where(x=>x.ProductId==input.ProductId);
            }
            if (!string.IsNullOrEmpty(input.Batch)) 
            {
                linq = linq.Where(x=>x.Batch==input.Batch);
            }
            if (input.Shelf_life > 0) 
            {
                linq=linq.Where(x=>x.Shelf_life==input.Shelf_life);
            }
            if (!string.IsNullOrEmpty(input.License)) 
            {
                linq=linq.Where(x=>x.License==input.License);
            }
            if (!string.IsNullOrEmpty(input.Enterprise)) 
            {
                linq=linq.Where(x=>x.Enterprise==input.Enterprise);
            }
            if (!string.IsNullOrEmpty(input.CreateUserName)) 
            {
                linq=linq.Where(x=>x.CreateUserName==input.CreateUserName);
            }
            if (!string.IsNullOrEmpty(input.UpdateUserName)) 
            {
                linq=linq.Where(x=>x.UpdateUserName==input.UpdateUserName);
            }
            var query = linq.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var list=_mapper.Map<List<ProductOutput>>(query);
            return new ApiPaging<ProductOutput> 
            {
                Code=ApiEnums.OK,
                Message="显示成功",
                TotalCount=linq.Count(),
                PageData=list
            };
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<int> Del(string id) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var info = 0;
            var ids=id.Split(',');
            foreach (var item in ids) 
            {
                var re = _traceRepository.GetModel(Convert.ToInt32(item));
                if (re != null) 
                {
                    re.IsDeleted = true;
                    info += _traceRepository.Update(re)>0 ? 1 : 0;
                }
            }
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "删除成功",
                Data = info
            };
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<TraceOutput> GetTraceShow(string Batch) 
        {
            ApiResult<TraceOutput> res = new ApiResult<TraceOutput>();
            var info = _traceRepository.GetAll().Where(x => x.Batch==Batch).FirstOrDefault();
            var good = _productRepository.GetAll().Where(x => x.Id == info.ProductId).FirstOrDefault();
            res = new ApiResult<TraceOutput>()
            {
                Code = ApiEnums.OK,
                Message = "查询成功",
                Data = new TraceOutput
                {
                    Id = info.Id,
                    ProductId = info.ProductId,
                    ProductName = good.ProductName,
                    Batch = info.Batch,
                    Shelf_life = info.Shelf_life,
                    License = info.License,
                    Enterprise = info.Enterprise,
                    Cover = info.Cover,
                }
            };
            return res;
               
                
            

        }

        /// <summary>
        /// 添加/修改养殖信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateCultivation(UpdateCultivationInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _cultivationRepository.GetAll().Where(x => x.Batch == input.Batch).FirstOrDefault();
            if (query != null)
            {
                var model = _mapper.Map(input, query);
                int i = _cultivationRepository.Update(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            else 
            {
                var model = _mapper.Map<CultivationModel>(input);
                int i = _cultivationRepository.Create(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            return res;
        }

        /// <summary>
        /// 获取养殖信息
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<CultivationModel>> GetCultivationShow(string batch) 
        {
            ApiResult<List<CultivationModel>> res = new ApiResult<List<CultivationModel>>();
            var info = _cultivationRepository.GetAll().Where(x => x.IsDeleted == false&&x.Batch==batch);
            res.Code= ApiEnums.OK;
            res.Message= "获取成功";
            res.Data = info.ToList();
            return res;
        }

        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddHealth(HealthInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _healthRepository.GetAll().Where(x => x.Batch == input.Batch).FirstOrDefault();
            var model=_mapper.Map<HealthModel>(input);
            int i=_healthRepository.Create(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "添加成功";
                res.Data = i;
            }
            else 
            {
                res.Code = ApiEnums.Fail;
                res.Message = "添加失败";
            }
            return res;
        }

        /// <summary>
        /// 修改保健信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> UpdateHealth(UpdateHealthInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _healthRepository.GetAll().Where(x => x.Batch == input.Batch).FirstOrDefault();
            var model = _mapper.Map<HealthModel>(input);
            int i = _healthRepository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "修改成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Fail;
                res.Message = "修改失败";
            }
            return res;
        }

        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<int> DeleteHealth(int id) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var model=_healthRepository.GetAll().FirstOrDefault(x => x.IsDeleted == false);
            model.IsDeleted = true;
            int i=_healthRepository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else 
            {
                res.Code = ApiEnums.Fail;
                res.Message = "删除失败";
            }
            return res;
        }

        /// <summary>
        /// 获取保健信息
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<HealthModel>> GetHealthShow(string batch) 
        {
            ApiResult<List<HealthModel>> res = new ApiResult<List<HealthModel>>();
            var info = _healthRepository.GetAll().Where(x => x.IsDeleted == false && x.Batch == batch);
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            res.Data = info.ToList();
            return res;
        }

        /// <summary>
        /// 添加/修改养殖企业信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddCultivation(CultivationInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _cultivation_TableRepository.GetAll().Where(x => x.Batch == input.Batch).FirstOrDefault();
            if (query != null)
            {
                var model = _mapper.Map(input, query);
                int i = _cultivation_TableRepository.Update(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            else
            {
                var model = _mapper.Map<Cultivation_TableModel>(input);
                int i = _cultivation_TableRepository.Create(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            return res;
        }

        /// <summary>
        /// 获取养殖企业信息
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public ApiResult<List<Cultivation_TableModel>> GetCultivationTableShow(string batch) 
        {
            ApiResult<List<Cultivation_TableModel>> res = new ApiResult<List<Cultivation_TableModel>>();
            var info = _cultivation_TableRepository.GetAll().Where(x => x.IsDeleted == false && x.Batch == batch);
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            res.Data = info.ToList();
            return res;
        }


        /// <summary>
        /// 添加/修改屠宰信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddSlaughte(SlaughteInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _slaughterRepository.GetAll().Where(x => x.Batch == input.Batch).FirstOrDefault();
            if (query != null)
            {
                var model = _mapper.Map(input, query);
                int i = _slaughterRepository.Update(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            else
            {
                var model = _mapper.Map<SlaughterModel>(input);
                int i = _slaughterRepository.Create(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            return res;
        }
        /// <summary>
        /// 获取屠宰信息
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public ApiResult<List<SlaughterModel>> GetSlaughteShow(string batch) 
        {
            ApiResult<List<SlaughterModel>> res = new ApiResult<List<SlaughterModel>>();
            var info = _slaughterRepository.GetAll().Where(x => x.IsDeleted == false && x.Batch == batch);
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            res.Data = info.ToList();
            return res;
        }


        /// <summary>
        /// 添加/修改屠宰企业信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddSlaughterTable(Slaughter_TableInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _slaughter_TableRepository.GetAll().Where(x => x.Batch == input.Batch).FirstOrDefault();
            if (query != null)
            {
                var model = _mapper.Map(input, query);
                int i = _slaughter_TableRepository.Update(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            else 
            {
                var model = _mapper.Map<Slaughter_TableModel>(input);
                int i = _slaughter_TableRepository.Create(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            return res;
        }

        /// <summary>
        /// 获取屠宰企业信息
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public ApiResult<List<Slaughter_TableModel>> GetSlaughterTableShow(string batch) 
        {
            ApiResult<List<Slaughter_TableModel>> res = new ApiResult<List<Slaughter_TableModel>>();
            var info = _slaughter_TableRepository.GetAll().Where(x => x.IsDeleted == false && x.Batch == batch);
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            res.Data = info.ToList();
            return res;
        }

        /// <summary>
        /// 添加/修改深加工信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddSlaughterhouse(SlaughterhouseInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _SlaughterhouseRepository.GetAll().Where(x => x.Batch == input.Batch).FirstOrDefault();
            if (query != null)
            {
                var model = _mapper.Map(input, query);
                int i = _SlaughterhouseRepository.Update(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            else
            {
                var model = _mapper.Map<SlaughterhouseModel>(input);
                int i = _SlaughterhouseRepository.Create(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            return res;
        }
        /// <summary>
        /// 获取深加工信息
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public ApiResult<SlaughterhouseOutput> GetSlaughterhouseShow(string batch) 
        {
            ApiResult<SlaughterhouseOutput> res = new ApiResult<SlaughterhouseOutput>();
            var info = _SlaughterhouseRepository.GetAll().Where(x => x.IsDeleted == false && x.Batch == batch).FirstOrDefault();
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            res.Data = new SlaughterhouseOutput()
            {
                Team = info.Team,
                Principal = info.Principal,
                Batch = info.Batch
            };
            return res;
        }

        /// <summary>
        /// 添加/修改深加工企业信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddSlaughterhouseTable(SlaughterhouseTableInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _SlaughterhouseRepository.GetAll().Where(x => x.Batch == input.Batch).FirstOrDefault();
            if (query != null)
            {
                var model = _mapper.Map(input, query);
                int i = _SlaughterhouseRepository.Update(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            else
            {
                var model = _mapper.Map<SlaughterhouseModel>(input);
                int i = _SlaughterhouseRepository.Create(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";

                }
            }
            return res;
        }
        /// <summary>
        /// 获取深加工信息
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public ApiResult<SlaughterhouseTableOutput> GetSlaughterhouseTableShow(string batch) 
        {
            ApiResult<SlaughterhouseTableOutput> res = new ApiResult<SlaughterhouseTableOutput>();
            var info = _SlaughterhouseRepository.GetAll().FirstOrDefault(x => x.IsDeleted == false && x.Batch == batch);
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            res.Data = new SlaughterhouseTableOutput()
            {
                CompanyName = info.CompanyName,
                License_Number = info.License_Number,
                Batch = info.Batch
            };
            return res;
        }


        /// <summary>
        /// 添加/修改供应商信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddSupplier(SupplierInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var query = _supplierRepository.GetAll().Where(x => x.Batch == input.Batch&&!x.IsDeleted&&x.SupplierBatch==input.SupplierBatch).FirstOrDefault();
            if (query != null)
            {
                var model = _mapper.Map(input, query);
                int i = _supplierRepository.Update(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                    return res;
                }
                else
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";
                }

            }
            else
            {
                var model = _mapper.Map<SupplierModel>(input);
                int i = _supplierRepository.Create(model);
                if (i > 0)
                {
                    res.Code = ApiEnums.OK;
                    res.Message = "保存成功";
                    res.Data = i;
                }
                else 
                {
                    res.Code = ApiEnums.Fail;
                    res.Message = "保存失败";
                }
                return res;
            }
            return res; 
        }
        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        public ApiResult<List<SupplierOutput>> GetSupplierShow(string batch) 
        {
            ApiResult<List<SupplierOutput>> res = new ApiResult<List<SupplierOutput>>();
            var info = _supplierRepository.GetAll().Where(x => x.IsDeleted == false && x.Batch == batch).ToList();
         
            res.Code = ApiEnums.OK;
            res.Message = "获取成功";
            res.Data=info.Select(x=>new SupplierOutput()
            {
                Batch = x.Batch,
                SupplierBatch = x.SupplierBatch,
                Type = x.Type,
            
            }).ToList();
            return res;
        }


        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> AddImg(ImageModel model) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var info=_mapper.Map<ImageModel>(model);
            info.IsDeleted = false;
            info.CreateUserName = "张三";
            info.UpdateUserName = "李四";
            var r=_imgRepository.Create(info);
            if (r > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "添加成功";
                res.Data = r;
            }
            else 
            {
                res.Code = ApiEnums.Fail;
                res.Message = "添加失败";
            }
            return res;
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ApiResult<int> DelImg(DelImgInput input) 
        {
            ApiResult<int> res = new ApiResult<int>();
            var info = _imgRepository.GetAll().Where(x => x.Id == input.Id && x.Batch == input.Batch && x.ImgType == input.ImgType).FirstOrDefault();
            if (info == null) 
            {
                res.Code = ApiEnums.Error;
                res.Message = "图片不存在";
                return res;
            }
            info.IsDeleted = true;
            int i=_imgRepository.Update(info);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else 
            {
                res.Code = ApiEnums.Fail;
                res.Message = "删除失败";
            }
            return res;
        }

        /// <summary>
        /// 获取图片
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<ImageModel>> GetImageShow(ImageOutput output) 
        {
            ApiResult<List<ImageModel>> res = new ApiResult<List<ImageModel>>();
            var model = _imgRepository.GetAll().Where(x => x.IsDeleted == false&&x.Batch==output.Batch&&x.ImgType==output.ImgType).ToList();
            res.Message = "查询成功";
            res.Code = ApiEnums.OK;
            res.Data = model;
            return res;
        }
    }
}
